//
//  AddressProvinceCityController.m
//  testAddress
//
//  Created by kinglian_iOS_000 on 2018/1/21.
//  Copyright © 2018年 AiShan. All rights reserved.
//

#import "AddressProvinceCityController.h"

#import "AddressProvinceCityModel.h"


static NSString *const kProvinceCellId = @"_provinceCellId_";

@interface AddressProvinceCityController () {
    NSDictionary *singleProvinceDict_;
}

@property (nonatomic,copy) NSString * parentId;

@property (nonatomic, assign) AddressVCType addressVCType;

@property (nonatomic, strong) NSArray *allProvinceArray;

@property (nonatomic,copy) NSString * titleName;

@end

@implementation AddressProvinceCityController

- (instancetype)initWithAddressVCType:(AddressVCType)addressVCType parentId:(NSString *)parentId titleName:(NSString *)titleName{
    if (self = [super init]) {
        self.addressVCType = addressVCType;
        self.parentId = parentId;
        if (titleName.length>0) {
            self.navigationItem.title = titleName;
            self.titleName = titleName;
        }else{
            self.navigationItem.title = @"所在地";
        }
        
        [self getListData];
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kProvinceCellId];
    self.tableView.tableFooterView = [[UIView alloc]init];
}


#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.allProvinceArray.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kProvinceCellId];
    
    AddressProvinceCityModel * model =  self.allProvinceArray[indexPath.row];
    cell.textLabel.text = model.name;
    cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;
 
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
        AddressProvinceCityModel * model =  self.allProvinceArray[indexPath.row];
    WEAKSELF
    switch (self.addressVCType) {
        case AddressVCTypeProvince: {
            
            AddressProvinceCityController *city = [[AddressProvinceCityController alloc] initWithAddressVCType:AddressVCTypeCity parentId:model.id titleName:model.name];
            [self.navigationController pushViewController:city animated:YES];
            
            [city setAddressCompleted:^(NSString *province, NSString *city) {
                if (weakSelf.addressCompleted) {
                    weakSelf.addressCompleted(province, city);
                    
                    NSArray *vcs = self.navigationController.childViewControllers;
                    [weakSelf.navigationController popToViewController:vcs[vcs.count - 3] animated:YES];
                }
            }];
            
            break;
        }
        case AddressVCTypeCity: {
            if (self.addressCompleted) {
                NSString *province = self.titleName;
                NSString *city = model.name;
                self.addressCompleted(province, city);
            }
        }
            break;
    }
    
}

//#pragma mark - Lazy
//- (NSArray *)allProvinceArray {
//    if (!_allProvinceArray) {
//        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"provinces_cities" ofType:@"plist"];
//        _allProvinceArray = [[NSArray alloc] initWithContentsOfFile:plistPath];
//    }
//    return _allProvinceArray;
//}


-(void)getListData{
    
    MJWeakSelf
    if (self.addressVCType == AddressVCTypeProvince) {
        
        [AppNetwork album_V2GetProvinceCityListWithDept:@"1" parentId:@"" Completion:^(BOOL success, NSArray *array, AppError *error) {
            if (success) {
                weakSelf.allProvinceArray = array;
                [weakSelf.tableView reloadData];
            }else{
                [KLProgressHUD showErrorHUDWithMessage:error.errorMessage];
            }
        }];
        
    }else{
        
        [AppNetwork album_V2GetProvinceCityListWithDept:@"2" parentId:self.parentId Completion:^(BOOL success, NSArray *array, AppError *error) {
            if (success) {
                weakSelf.allProvinceArray = array;
                [weakSelf.tableView reloadData];
            }else{
                [KLProgressHUD showErrorHUDWithMessage:error.errorMessage];
            }
        }];
        

        
    }

}

@end
